//
//  GXCycleImage.m
//  16-UICollectionViewCycleImage
//
//  Created by Snow on 16/8/4.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "GXCycleImage.h"

#define kColor [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1]
#define Reuse @"cell"
@interface GXCycleImage ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) UICollectionView *cycleCollectionView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSTimeInterval time;
@end
@implementation GXCycleImage
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        _time = 1;
        self.userInteractionEnabled = YES;
        [self setup];
    }
    [self startTime];
    
    return self;
}
- (void)startTime
{
    //创建一个timer 并添加到loop中
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:_time target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];
    //timer 仅仅是创建timer 并没有加入loop
    _timer = [NSTimer timerWithTimeInterval:_time target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];
    //nsrunloopcommonmodes nsdefalutrunloop
    //后者代表默认状态 scheduledTimerWithTimeInterval添加为默认状态
    //前者为多种状态 包含默认 以及 滚动
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
//    [self removeTimer];
}
- (void)removeTimer
{
    //取消timer时  调用invalidate
    [_timer invalidate];
    //timer置空
    _timer = nil;
}
- (void)timeAction:(NSTimer *)time
{
    [_cycleCollectionView setContentOffset:CGPointMake((_cycleCollectionView.contentOffset.x + self.frame.size.width), 0)];
    if (_cycleCollectionView.contentOffset.x == self.frame.size.width * (_pageControl.numberOfPages + 1)) {
        [_cycleCollectionView setContentOffset:CGPointMake(self.frame.size.width, 0)];
    }
    _pageControl.currentPage = (_cycleCollectionView.contentOffset.x + self.frame.size.width / 2) / self.cycleCollectionView.frame.size.width - 1;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor greenColor];
        [self setup];
        NSLog(@"%lf", self.frame.size.width);
    }
    return self;
}
- (void)setup
{
    [self createCollectionView];
    [self createPageControl];
}
- (void)createCollectionView
{
    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.minimumInteritemSpacing = 0;
    _cycleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
    _cycleCollectionView.delegate = self;
    _cycleCollectionView.dataSource = self;
    _cycleCollectionView.pagingEnabled = YES;
    [self addSubview:_cycleCollectionView];
    [_cycleCollectionView registerClass:[GXCycleImageCell class] forCellWithReuseIdentifier:Reuse];
    [_cycleCollectionView setContentOffset:CGPointMake(self.frame.size.width, 0)];
    _pageControl = [[UIPageControl alloc] init];
    [self addSubview:_pageControl];
    [_pageControl addTarget:self action:@selector(handlePage:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)handlePage:(UIPageControl *)page
{
    [_cycleCollectionView setContentOffset:CGPointMake(_cycleCollectionView.frame.size.width * (page.currentPage + 1), 0) animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _pageControl.currentPage =(scrollView.contentOffset.x + self.frame.size.width / 2) / self.cycleCollectionView.frame.size.width - 1;
    if (scrollView.contentOffset.x == 0) {
//        [_cycleCollectionView setContentOffset:CGPointMake(_cycleCollectionView.bounds.size.width * (_arrayImage.count - 2), 0)];
        [_cycleCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_arrayImage.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
        _pageControl.currentPage = _arrayImage.count - 3;
    } else if (scrollView.contentOffset.x == _cycleCollectionView.bounds.size.width * (_arrayImage.count - 1)) {
//        [_cycleCollectionView setContentOffset:CGPointMake(_cycleCollectionView.bounds.size.width, 0)];
        [_cycleCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
        _pageControl.currentPage = 0;

    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayImage.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GXCycleImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Reuse forIndexPath:indexPath];
    cell.cycleImage.image =[UIImage imageNamed:[_arrayImage objectAtIndex:indexPath.row]];
    cell.cycleLabel.text = [_arrayLabel objectAtIndex:indexPath.row ];
//    if (self.delesource && [self.delesource respondsToSelector:@selector(handleObject:cell:)]) {
//        [self.delesource handleObject:_arrayImage[indexPath.row] cell:cell];
//    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delaget && [self.delaget respondsToSelector:@selector(pressCollectionView)]) {
        [self.delaget pressCollectionView];
    } ;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _cycleCollectionView.frame = self.bounds;
    _flowLayout.itemSize = self.bounds.size;
    _pageControl.frame = CGRectMake(0, self.bounds.size.height - 30, self.bounds.size.width, 30);
}
- (void)createPageControl
{

}
//set 赋值
- (void)setArrayImage:(NSMutableArray *)arrayImage
{
    _arrayImage = arrayImage;
    _pageControl.numberOfPages = _arrayImage.count - 2;
}
- (void)setArrayLabel:(NSMutableArray *)arrayLabel
{
    _arrayLabel = arrayLabel;
}
@end

@implementation GXCycleImageCell
- (instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _cycleImage = [[UIImageView alloc] initWithFrame:self.bounds];
        _cycleImage.userInteractionEnabled = YES;
        [self.contentView addSubview:_cycleImage];
        _cycleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 60, self.bounds.size.width, 30)];
        [self.contentView addSubview:_cycleLabel];
    }
    return self;
}
@end



